package znbkListRdtz.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.example.lancooznbk.R;
import znbkListRdtz.entity.HeatPointExtensionListEntity;
import znbkListRdtz.presenter.HeatPointExtensionListPresenter;
import znbkListRdtz.adapter.HeatPointExtensionListAdapter;
import znbkTestPaperRdtz.entity.HeatPointExtensionPaperEntity;

import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\2\5 0005.
 */

public class HeatPointExtensionListFragment extends BaseMainFragment<HeatPointExtensionListPresenter> implements View.OnClickListener,ILoadMoreDataView<HeatPointExtensionListEntity> {




    private HeatPointExtensionListAdapter mHeatPointExtensionListAdapter;

//    private TextView mTvSpStatus;
    private TextView mTvSpQuesType;
    private TextView mTvSpQuesRank;
    private TextView mTvTotal;
    private PopupMenuType1Adapter popupMenuStatusAdapter;
    private PopupMenu statusPopupMenu;
    private PopupMenuType1Adapter popupMenuQuesTypeAdapter;
    private PopupMenu quesTypePopupMenu;
    private PopupMenuType1Adapter popupMenuQuesRankAdapter;
    private PopupMenu quesRankPopupMenu;
    private View masker;
    private RelativeLayout mSpLayout;


    private Map<String,Integer> mQuesStatusMap = new HashMap<>();
    private Map<String,String> mQuesTypeMap = new HashMap<>();
    private Map<String,Integer> mQuesRankMap = new HashMap<>();

    public static int ResStatusFlag = 0;

    private int checkPosition = -1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_heat_point_extension;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {

        checkPosition = 0;

        mPresenter = new HeatPointExtensionListPresenter(this);

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpQuesType = (TextView) view.findViewById(R.id.sp_quesType);
        mTvSpQuesRank = (TextView) view.findViewById(R.id.sp_quesRank);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        masker = view.findViewById(R.id.bg_masker);

        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_rdtz);
        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

        /*
        * 资源类型
        * */
        mTvSpQuesType.setOnClickListener(this);


        /*
        * 排序
        * */
        mTvSpQuesRank.setOnClickListener(this);

        String[] quesRanksArrays = getResources().getStringArray(R.array.QuesRanksArraysRdtz);
        String[] quesRanksName = new String[quesRanksArrays.length];
        for (int i = 0 ; i < quesRanksArrays.length ; i++){
            String[] split = quesRanksArrays[i].split("\\|");
            quesRanksName[i] = split[0];
            mQuesRankMap.put(split[0], Integer.valueOf(split[1]));
        }

        popupMenuQuesRankAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesRanksName);
        quesRankPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        quesRankPopupMenu.setListAdapter(popupMenuQuesRankAdapter);
        quesRankPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpQuesRank.setText(quesRankPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesRank(mQuesRankMap.get(quesRankPopupMenu.getCheckedStr()));
            }
        });
        mTvSpQuesRank.setText(quesRankPopupMenu.getCheckedStr());
        quesRankPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpQuesRank.setSelected(false);
            }
        });


        mHeatPointExtensionListAdapter = new HeatPointExtensionListAdapter(getActivity());
        mHeatPointExtensionListAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mHeatPointExtensionListAdapter);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));//这里用线性宫格显示 类似于grid view
        mHeatPointExtensionListAdapter.setOnItemClickListener(new HeatPointExtensionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<HeatPointExtensionListEntity.ResourceBean> list, int position) {
                mPresenter.openItemDetail(position,list.get(position));
            }
        });

    }


    @Override
    public void showNetError() {
        super.showNetError();
        setPaperTotal(0);
        Log.e("==showNetError===","");
    }


    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setPaperTotal(0);
        Log.e("==showNoData===","");
    }


    @Override
    public void loadData(HeatPointExtensionListEntity data) {

        Log.e("==loadData===",""+data.getTotalCount());

        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }


        String[] quesTypeNames = new String[data.getResTypeList().size()+1];
        String[] quesTypeTypeIDs = new String[data.getResTypeList().size()+1];

        for (int i = 0 ; i < data.getResTypeList().size()+1 ; i++){
            if(i==0){
                quesTypeNames[i] = "全部题型";
                quesTypeTypeIDs[i] = "";
            }else {
                quesTypeNames[i] = data.getResTypeList().get(i-1).getResTypeName();
                quesTypeTypeIDs[i] = data.getResTypeList().get(i-1).getResType();
            }
            mQuesTypeMap.put(quesTypeNames[i],quesTypeTypeIDs[i]);
        }


        popupMenuQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames,checkPosition);
        quesTypePopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        quesTypePopupMenu.setListAdapter(popupMenuQuesTypeAdapter);
        quesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                checkPosition = position;
                mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(quesTypePopupMenu.getCheckedStr()));
            }
        });

        quesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpQuesType.setSelected(false);
            }
        });


        Log.e("==count===",""+data.getTotalCount());

        setPaperTotal(data.getTotalCount());
        mHeatPointExtensionListAdapter.clearAndAdd(data.getResource());
        loadComplete(mHeatPointExtensionListAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(HeatPointExtensionListEntity data) {
        setPaperTotal(data.getTotalCount());
        mHeatPointExtensionListAdapter.add(data.getResource());
        loadComplete(mHeatPointExtensionListAdapter.getAdapterItemCount() >= data.getTotalCount());
    }



    /**
     * 没有试卷或者加载失败的逻辑处理
     */
    private void setPaperTotal(int number) {
        Log.e("==setPaperTotal===",""+number);
        String paperTotal = String.format(getString(R.string.znbkResTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }



  /*  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main,menu);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.item_other_system){
            //ToastUtils.showShort("难点进度提示");
            startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
            return true;
        }
        return false;
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.sp_status:
//                masker.setVisibility(View.VISIBLE);//阴影布局可见
//                mTvSpStatus.setSelected(true);
//                statusPopupMenu.showLocation(mSpLayout, 0, -2);
//                break;
            case R.id.sp_quesType:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpQuesType.setSelected(true);
                quesTypePopupMenu.showLocation(mSpLayout, 0, -2);
                break;
            case R.id.sp_quesRank:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpQuesRank.setSelected(true);
                quesRankPopupMenu.showLocation(mSpLayout, 0, -2);
                break;

            case R.id.layout_toolbar_back:
                getHolderActivity().finish();
                break;
//            case R.id.img_main_menu:
//                startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
//                break;
         /* case R.id.fab_coping:
              BaseReviewWrongActivity.launch(v.getContext(),1);
              break;*/
        }
    }



}